package com.briup.estore.adminAPI.web.controller;

import com.briup.estore.common.bean.Shop;
import com.briup.estore.common.service.IShopService;
import com.briup.estore.common.util.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @author adam
 * @date 2022/10/13
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "商品管理")
@CrossOrigin
public class ShopController {
    @Autowired
    private IShopService shopService;

    @GetMapping("/findShopInfo")
    @ApiOperation("查询商品信息 带分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query", required = false, defaultValue = "", value = "搜索关键字可以为空", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", required = true, value = "当前页面数据条数", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", required = true, value = "当前页数", paramType = "query"),
    })
    public Result findShopInfo(@RequestParam(required = false, defaultValue = "") String query, Integer pageNum, Integer pageSize) {
        try {
            Page<Shop> shops = shopService.findShops(query, pageNum, pageSize);
            return Result.success(shops);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(8000, e.getMessage());
        }

    }

    @PostMapping("/saveShop")
    @ApiOperation("新增/修改商品  修改商品时需要商品的全部参数")

    public Result saveShop(@RequestBody Shop shop) {
        shopService.save(shop);
        return Result.success();
    }

    @DeleteMapping("updateShopStatus/{id}")
    @ApiOperation("修改商品的在售状态 ")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, value = "商品id", paramType = "path", name = "id")
    })
    public Result updateShopStatus(@PathVariable Long id) {
        shopService.updateShopStatus(id);
        return Result.success();
    }


    @GetMapping("getShopInfo/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, paramType = "path", value = "商品id", name = "id")
    })
    public Result getShopInfo(@PathVariable Long id) {
        Shop shop = shopService.findShopById(id);
        System.out.println("shop = " + shop);
        return Result.success(shop);

    }

}
